import {BaseApi} from "~/api/util/BaseService";
import {User} from "~/api/system/user/types";
import {reactive} from "vue";
import {ElMessage} from "element-plus";

export class BindApi extends BaseApi<User> {

    baseUrl = "/system/account";

    changPasswordState = reactive({
        visible: false,
        loading: false,
        form: {
            id: "",
            code: "",
            codeType: "",
            password: "",
            passwordAgain: "",
        },
    });
    bindPhoneState = reactive({
        loading: false,
        visible: false,
        form: {
            userId: "",
            phone: "",
            code: "",
        },
    });
    bindEmailState = reactive({
        loading: false,
        visible: false,
        form: {
            userId: "",
            email: "",
            code: "",
        },
    });

    onShowChangePassword(id: string) {
        this.changPasswordState.visible = true;
        this.changPasswordState.form = {
            id: id,
            code: "",
            codeType: "",
            password: "",
            passwordAgain: "",
        };
    }

    onChangPassword() {
        this.changPasswordState.loading = true;
        this.post("/changPassword", this.changPasswordState.form).then(res => {
            if (res.ok) {
                ElMessage.success("修改成功");
                this.onCloseChangPassword();
            }
        }).finally(() => {
            this.changPasswordState.loading = false;
        });
    }

    onCloseChangPassword() {
        this.changPasswordState.visible = false;
        this.changPasswordState.form = {
            id: "",
            code: "",
            codeType: "",
            password: "",
            passwordAgain: "",
        };
    }

    onShowBindPhone(userId: string) {
        this.bindPhoneState.visible = true;
        this.bindPhoneState.form.userId = userId;
    }

    onCloseBindPhone() {
        this.bindPhoneState.visible = false;
        this.bindPhoneState.form = {
            userId: "",
            phone: "",
            code: "",
        };
    }

    onBindPhone() {
        return new Promise((resolve, reject) => {
            this.bindPhoneState.loading = true;
            this.post("/bindPhone", this.bindPhoneState.form).then(res => {
                if (res.ok) {
                    ElMessage.success("绑定成功");
                    this.onCloseBindPhone();
                    resolve(res);
                } else {
                    reject(res);
                }
            }).catch((error) => {
                reject(error);
            }).finally(() => {
                this.bindPhoneState.loading = false;
            });
        });
    }

    onShowBindEmail(userId: string) {
        this.bindEmailState.visible = true;
        this.bindEmailState.form.userId = userId;
    }

    onCloseBindEmail() {
        this.bindEmailState.visible = false;
        this.bindEmailState.form = {
            userId: "",
            email: "",
            code: "",
        };
    }

    onBindEmail() {
        return new Promise((resolve, reject) => {
            this.bindEmailState.loading = true;
            this.post("/bindEmail", this.bindEmailState.form).then(res => {
                if (res.ok) {
                    ElMessage.success("绑定成功");
                    this.onCloseBindEmail();
                    resolve(res);
                } else {
                    reject(res);
                }
            }).catch((error) => {
                reject(error);
            }).finally(() => {
                this.bindEmailState.loading = false;
            });
        });
    }
}
